package com.ibox.core.book.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.request.RequestOptions;
import com.ibox.core.book.R;
import com.ibox.core.book.bean.TopRankList;
import com.ibox.core.book.utils.BookConstant;
import com.ibox.widget.GlideCircleTransform;

import java.util.List;

/**
 * Project IBox
 * Created by Amos
 * Created on 2017-10-12
 * Desc
 */

public class TopRankAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private LayoutInflater mInflater;

    private List<TopRankList.MaleBean> mGroupList;
    private List<List<TopRankList.MaleBean>> mChildList;

    public TopRankAdapter(Context context, List<TopRankList.MaleBean> groupList,
                          List<List<TopRankList.MaleBean>> childList) {
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mGroupList = groupList;
        mChildList = childList;
    }

    @Override
    public int getGroupCount() {
        return mGroupList == null ? 0 : mGroupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mChildList == null ? 0 : mChildList.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mGroupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mChildList.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View group = mInflater.inflate(R.layout.item_top_rank_group, null);
        ImageView ivRankArrow = group.findViewById(R.id.ivRankArrow);
        TextView tvRankGroupName = group.findViewById(R.id.tvRankGroupName);
        ImageView ivRankCover = group.findViewById(R.id.ivRankCover);
        TopRankList.MaleBean maleBean = mGroupList.get(groupPosition);
        if (!TextUtils.isEmpty(maleBean.cover)) {
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.mipmap.ic_launcher)
                    .error(R.mipmap.ic_launcher)
                    .transform(new GlideCircleTransform())
                    .priority(Priority.HIGH);
            Glide.with(mContext).load(BookConstant.BOOK_IMAGE_BASE_URL + maleBean.cover)
                    .apply(options).into(ivRankCover);
        } else {
            ivRankCover.setImageResource(R.drawable.ic_rank_collapse);
        }
        tvRankGroupName.setText(maleBean.getTitle());
        if (mChildList.get(groupPosition).size() > 0) {
            if (isExpanded) {
                ivRankArrow.setImageResource(R.drawable.rank_arrow_up);
            } else {
                ivRankArrow.setImageResource(R.drawable.rank_arrow_down);
            }
        } else {
            ivRankArrow.setVisibility(View.GONE);
        }
        return group;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild,
                             View convertView, ViewGroup parent) {

        View child = mInflater.inflate(R.layout.item_top_rank_child, null);
        TextView tvName = child.findViewById(R.id.tvRankChildName);
        TopRankList.MaleBean maleBean = mChildList.get(groupPosition).get(childPosition);
        tvName.setText(maleBean.title);
        child.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(groupPosition, childPosition);
                }
            }
        });
        return child;
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        mOnItemClickListener = itemClickListener;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public interface OnItemClickListener {
        void onItemClick(int groupPosition, int childPosition);
    }
}
